extends Node

var player_res := preload("res://entity/player/player.tscn")
#GUI 状态区分
var current_state = GameState.HOME:
	set(v):
		if v == current_state: return
		last_state = current_state
		current_state = v
var last_state = GameState.HOME
enum GameState{
	HOME,
	GAMING,
	SELECT_CHARACTOR,
	MENU,
	GAME_OVER,
}


var controller_arr: Dictionary = {
	"key_": "键盘",
	"joy0_": "手柄1",
	"joy1_": "手柄2",
	"joy2_": "手柄3",
}
var player_data: Array = [
	{"modulate": [1.0, 1.0, 1.0], "name": "普通鸟"},
	{"modulate": [1.0, 0.0, 1.0], "name": "紫鸟"},
	{"modulate": [1.0, 0.0, 0.0], "name": "红鸟"},
	{"modulate": [0.0, 0, 1.0], "name": "蓝鸟"},
]
var player_id_map: Dictionary = {} ## key_ : id
var bird_dir := preload("res://entity/bird_die/bird_die.tscn")

var players: Array[Player] = []
var ui: Ui
var game_camera: Camera2D

var is_game_pause := false #暂停游戏
var is_game_start := false #是否开始


func make_player() -> void:
	for key in player_id_map.keys():
		var exist := false
		for p in players:
			if p.device == key:
				p.charactor_id = player_id_map[key]
				exist = true
		if not exist:
			var new_player: Player = player_res.instantiate()
			var modulate_data: Array = player_data[player_id_map[key]]["modulate"]
			new_player.get_node("Graphics").modulate = Color(modulate_data[0], modulate_data[1], modulate_data[2])
			new_player.position = Vector2(player_id_map[key] * 200, 0)
			new_player.device = key
			new_player.charactor_id = player_id_map[key]
			players.append(new_player)
			add_to_root("World/Players", new_player, Vector2.ZERO, Vector2(0.2, 0.2))
	
## 暂停游戏
func pause_game(is_pause: bool) -> void:
	if not is_game_start: return
	if is_pause:
		Engine.time_scale = 0
		is_game_pause = true
		SoundManager.play_sfx("BgMusic", -1)
		ui.show_pause_menu(true)
	else:
		Engine.time_scale = 1
		is_game_pause = false
		SoundManager.play_sfx("BgMusic", 1)
		ui.show_pause_menu(false)


## 是否在游戏
func is_gaming() -> bool:
	return Game.is_game_start and is_game_pause == false


func init_one_player() -> void:
	player_id_map["key_"] = 0

## 开始游戏
func start_game() -> void:
	if is_game_pause:
		pause_game(false)
	else:
		if not player_id_map.size(): init_one_player()
		make_player()
		is_game_start = true
		SoundManager.play_sfx("BgMusic")
		for p in players:
			p.reset()
		ui.show_gaming()

## 重新开始游戏
func restart_game() -> void:
	get_tree().reload_current_scene()

## 退出游戏
func exit_game() -> void:
	get_tree().quit()

## 玩家死亡
func player_die(player: Player) -> void:
	players.erase(player)
	player.queue_free()
	add_to_root("World", bird_dir.instantiate(), player.global_position, Vector2.ONE)
	SoundManager.play_sfx("Impact")
	
	if not players.size():
		Game.game_camera.shake()
		SoundManager.play_sfx("BgMusic", true)
		ui.show_game_over()
		is_game_start = false


func add_to_root(child: String, node: Node, position: Vector2, scale: Vector2) -> void:
	if position.length(): node.global_position = position
	if scale.length(): node.scale = scale
	var container := get_tree().root if child == "" else get_node("/root/" + child)
	container.add_child(node)
